package Define_generic_struct

import (
	"fmt"
	"sync"
)

type myList[T int | string | int32 | float32 | float64] struct {
	data     []T
	max, min T
	lock     sync.Mutex
}

// 添加元素
func (l *myList[T]) Add(ele T) *myList[T] {
	l.lock.Lock()
	defer l.lock.Unlock()

	l.data = append(l.data, ele)

	if len(l.data) == 1 {
		l.max = ele
		l.min = ele
	}

	if len(l.data) > 1 {
		if l.max < ele {
			l.max = ele
		}

		if l.min > ele {
			l.min = ele
		}
	}

	return l
}

func (l myList[T]) Max() T {
	return l.max
}

func (l myList[T]) Min() T {
	return l.min
}

func (l myList[T]) GetData() []T {
	return l.data
}

func GenericStructTest() {
	mlInt := myList[int]{}
	mlInt.Add(1).Add(3).Add(10)
	fmt.Println(mlInt.GetData())
	fmt.Println("mlIntMax: ", mlInt.Max())
	fmt.Println("mlIntMin: ", mlInt.Min())

	fmt.Println("=======================================")

	mlString := myList[string]{}
	mlString.Add("hahaha").Add("hehehe").Add("ooo")
	fmt.Println(mlString.GetData())
	fmt.Println("mlStringMax: ", mlString.Max())
	fmt.Println("mlStringMin: ", mlString.Min())

	//var s2 string = string("dsadwdwad")
	//s2[2] = 'a'
	//
	//var s1 string = "adwd"
	//s1[1] = 'a'
}
